package com.myf.warehouse3d.controllers;

import com.myf.ApiResult;
import com.myf.Utility;
import com.myf.dtos.warehouse.WarehouseInput;
import com.myf.dtos.warehouse.WarehouseQueryInput;
import com.myf.dtos.warehouse.WarehouseQueryOutput;
import com.myf.features.BaseController;
import com.myf.services.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/api/warehouse")
@Tag(name = "仓库功能", description = "仓库相关API接口")
public class WarehouseController extends BaseController {
    private final WarehouseService warehouseService;

    public WarehouseController(WarehouseService warehouseService) {
        this.warehouseService = warehouseService;
    }

    /// 保存
    @Operation(summary = "新建或保存仓库接口")
    @PutMapping("save")
    public ApiResult save(@RequestBody WarehouseInput warehouseInput) {
        try {
            return ApiResult.createSuccess(warehouseService.save(warehouseInput, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "分页查询仓库列表接口")
    @PostMapping("query")
    public ApiResult query(@RequestBody WarehouseQueryInput input) {
        try {
            return ApiResult.createSuccess(warehouseService.query(input));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "通过仓库id获取详细信息接口")
    @GetMapping("{id}")
    public ApiResult get(@PathVariable int id) {
        try {
            var data = warehouseService.getById(id);
            var output = new WarehouseQueryOutput();
            output.id = data.id;
            output.name = data.name;
            output.status = data.status;
            output.code = data.code;
            output.buildingStructure = data.buildingStructure;
            output.category = data.category;
            output.address = data.address;
            output.contactMobileNo = data.contactMobileNo;
            output.contactPeople = data.contactPeople;
            output.fireProtectionRating = data.fireProtectionRating;
            output.fireResistanceRating = data.fireResistanceRating;
            output.length = data.length;
            output.width = data.width;
            output.height = data.height;
            return ApiResult.createSuccess(output);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "根据仓库id集合批量删除仓库接口")
    @DeleteMapping()
    public ApiResult delete(@RequestParam String ids) {
        try {
            var toDeleteIds = new ArrayList<Integer>();
            var temp = ids.split(",");
            for (String id : temp) {
                var res = Utility.tryParse(id);
                if (res != null) {
                    toDeleteIds.add(res);
                }
            }
            if (toDeleteIds.isEmpty()) {
                return ApiResult.createSuccess(0);
            }
            return ApiResult.createSuccess(warehouseService.delete(toDeleteIds, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "获取仓库下拉列表框中数据的接口")
    @GetMapping("items")
    public ApiResult getForSelectItems(@RequestParam(required = false) String keyword) {
        try {
            return ApiResult.createSuccess( warehouseService.getForSelectItems(keyword));
        }
        catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }
}
